<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>odedc</title>
  </head>
  <body bgcolor="#FFFFFF">
    <center>Scilab Function</center>
    <div align="right">Last update : April 1993</div>
    <p>
      <b>odedc</b> -  discrete/continuous ode solver</p>
    <h3>
      <font color="blue">Calling Sequence</font>
    </h3>
    <dl>
      <dd>
        <tt>yt=odedc(y0,nd,stdel,t0,t,f)  </tt>
      </dd>
    </dl>
    <h3>
      <font color="blue">Parameters</font>
    </h3>
    <ul>
      <li>
        <tt>
          <b>y0</b>
        </tt>: real column vector (initial conditions), <tt>
          <b>y0=[y0c;y0d]</b>
        </tt> where <tt>
          <b>y0d</b>
        </tt> has <tt>
          <b>nd</b>
        </tt> components.</li>
      <li>
        <tt>
          <b>nd</b>
        </tt>: integer, dimension of <tt>
          <b>y0d</b>
        </tt>
      </li>
      <li>
        <tt>
          <b>stdel</b>
        </tt>: real vector with one or two entries, <tt>
          <b>stdel=[h, delta]</b>
        </tt> (with <tt>
          <b>delta=0</b>
        </tt> as default value).</li>
      <li>
        <tt>
          <b>t0</b>
        </tt>: real scalar (initial time).</li>
      <li>
        <tt>
          <b>t</b>
        </tt>: real (row) vector, instants where <tt>
          <b>yt</b>
        </tt> is calculated .</li>
      <li>
        <tt>
          <b>f</b>
        </tt>: external i.e. function or character string or list with calling sequence: <tt>
          <b>yp=f(t,yc,yd,flag)</b>
        </tt>.</li>
    </ul>
    <h3>
      <font color="blue">Description</font>
    </h3>
    <p>
      <tt>
        <b>y=odedc([y0c;y0d],nd,[h,delta],t0,t,f)</b>
      </tt>
    computes the solution of a mixed discrete/continuous system. 
    The discrete system state <tt>
        <b>yd_k</b>
      </tt> is embedded into a  piecewise constant <tt>
        <b>yd(t)</b>
      </tt> time function as follows:</p>
    <pre>

yd(t)=yd_k for t in 
[t_k=delay+k*h,t_(k+1)=delay+(k+1)*h[ (with delay=h*delta).
   
    </pre>
    <p>
    The simulated equations are now:</p>
    <pre>

dyc/dt=f(t,yc(t),yd(t),0),  for t in [t_k,t_(k+1)[
yc(t0)=y0c
   
    </pre>
    <p>
    and at instants <tt>
        <b>t_k</b>
      </tt> the discrete variable <tt>
        <b>yd</b>
      </tt> is updated by:</p>
    <pre>

yd(t_k+)=f(yc(t_k-),yd(t_k-),1)
   
    </pre>
    <p>
    Note that, using the definition of <tt>
        <b>yd(t)</b>
      </tt> the last equation gives</p>
    <pre>

yd_k = f (t_k,yc(t_k-),yd(t_(k-1)),1)  (yc is time-continuous: yc(t_k-)=yc(tk))
   
    </pre>
    <p>
    The calling parameters of <tt>
        <b>f</b>
      </tt> are fixed: <tt>
        <b>ycd=f(t,yc,yd,flag)</b>
      </tt>;
    this function must return either the derivative of the vector <tt>
        <b>yc</b>
      </tt> if
    <tt>
        <b>flag=0</b>
      </tt> or the update of <tt>
        <b>yd</b>
      </tt> if <tt>
        <b>flag=1</b>
      </tt>.</p>
    <p>
      <tt>
        <b>ycd=dot(yc)</b>
      </tt> must be a vector with same dimension as <tt>
        <b>yc</b>
      </tt> 
    if <tt>
        <b>flag=0</b>
      </tt> and <tt>
        <b>ycd=update(yd)</b>
      </tt> must be a vector with same 
    dimension as <tt>
        <b>yd</b>
      </tt> if <tt>
        <b>flag=1</b>
      </tt>.</p>
    <p>
      <tt>
        <b>t</b>
      </tt> is a vector of instants where the solution <tt>
        <b>y</b>
      </tt> is computed.</p>
    <p>
      <tt>
        <b>y</b>
      </tt> is the vector <tt>
        <b>y=[y(t(1)),y(t(2)),...]</b>
      </tt>.
    This function can be called with the same optional parameters as the
    <tt>
        <b>ode</b>
      </tt> function (provided <tt>
        <b>nd</b>
      </tt> and <tt>
        <b>stdel</b>
      </tt> are given
    in the calling sequence as second and third parameters).
    In particular integration flags, tolerances can be set. Optional
    parameters can be set by the <tt>
        <b>odeoptions</b>
      </tt> function.</p>
    <p>
    An example for calling an external routine is given in directory 
    <tt>
        <b>SCIDIR/default/fydot2.f</b>
      </tt>
    </p>
    <p>
    External routines can be dynamically linked (see <tt>
        <b>link</b>
      </tt>).</p>
    <h3>
      <font color="blue">Examples</font>
    </h3>
    <pre>

//Linear system with switching input
deff('xdu=phis(t,x,u,flag)','if flag==0 then xdu=A*x+B*u; else xdu=1-u;end');
x0=[1;1];A=[-1,2;-2,-1];B=[1;2];u=0;nu=1;stdel=[1,0];u0=0;t=0:0.05:10;
xu=odedc([x0;u0],nu,stdel,0,t,phis);x=xu(1:2,:);u=xu(3,:);
nx=2;
plot2d1('onn',t',x',[1:nx],'161');
plot2d2('onn',t',u',[nx+1:nx+nu],'000');
//Fortran external( see fydot2.f): 
norm(xu-odedc([x0;u0],nu,stdel,0,t,'phis'),1)

//Sampled feedback 
//
//        |     xcdot=fc(t,xc,u)
//  (system)   |
//        |     y=hc(t,xc)
//
//
//        |     xd+=fd(xd,y)
//  (feedback) |
//        |     u=hd(t,xd)
//
deff('xcd=f(t,xc,xd,iflag)',...
  ['if iflag==0 then '
   '  xcd=fc(t,xc,e(t)-hd(t,xd));'
   'else '
   '  xcd=fd(xd,hc(t,xc));'
   'end']);
A=[-10,2,3;4,-10,6;7,8,-10];B=[1;1;1];C=[1,1,1];
Ad=[1/2,1;0,1/20];Bd=[1;1];Cd=[1,1];
deff('st=e(t)','st=sin(3*t)')
deff('xdot=fc(t,x,u)','xdot=A*x+B*u')
deff('y=hc(t,x)','y=C*x')
deff('xp=fd(x,y)','xp=Ad*x + Bd*y')
deff('u=hd(t,x)','u=Cd*x')
h=0.1;t0=0;t=0:0.1:2;
x0c=[0;0;0];x0d=[0;0];nd=2;
xcd=odedc([x0c;x0d],nd,h,t0,t,f);
norm(xcd-odedc([x0c;x0d],nd,h,t0,t,'fcd1')) // Fast calculation (see fydot2.f)
plot2d([t',t',t'],xcd(1:3,:)');
xset("window",2);plot2d2("gnn",[t',t'],xcd(4:5,:)');
xset("window",0);
 
  </pre>
    <h3>
      <font color="blue">See Also</font>
    </h3>
    <p>
      <a href="ode.htm">
        <tt>
          <b>ode</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="odeoptions.htm">
        <tt>
          <b>odeoptions</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="../control/csim.htm">
        <tt>
          <b>csim</b>
        </tt>
      </a>,&nbsp;&nbsp;<a href="../programming/external.htm">
        <tt>
          <b>external</b>
        </tt>
      </a>,&nbsp;&nbsp;</p>
  </body>
</html>
